package br.ufrgs.inf.sissw.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import br.ufrgs.inf.sissw.app.interfaces.AgendaCustomArrayAdapter;
import br.ufrgs.inf.sissw.app.models.AgendaItem;

public class AgendaActivity extends AbstractDashboardActivity implements OnItemClickListener, OnClickListener {
	
	private ImageButton buttonAddNewItem;	
	ListView listViewBody;
	AgendaCustomArrayAdapter itemsArrayAdapter;
	List<AgendaItem> itemsList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		buttonAddNewItem = (ImageButton)findViewById(R.id.buttonAddNewItem);
		buttonAddNewItem.setOnClickListener(this);
		
		listViewBody = (ListView)findViewById(R.id.listViewItemsList);
		
		
		itemsList = new ArrayList<AgendaItem>();
        AgendaItem newItem = new AgendaItem("Apresentação de Sistemas de software");
        itemsList.add(newItem);
        itemsArrayAdapter = new AgendaCustomArrayAdapter(this, R.layout.agenda_row_layout, itemsList);
        
        listViewBody.setAdapter(itemsArrayAdapter);
        try {
        	listViewBody.setOnItemClickListener(this);
        }
        catch (Exception e) {
        	System.out.println("Quebrou tudo");
        	System.out.println(e.getMessage());
        	System.out.println("Quebrou tudo");
        }
        //listViewBody.seton(listener)
        //listViewBody.setAdapter(new ArrayAdapter<String>(this, R.layout.agenda_row_layout, R.id.editTextItemTitle, teste));
		
	};
	
	@Override
	protected int getContentView() {
		return R.layout.agenda_activity;
	}

	@Override
	protected int getFeatureNameResourceId() {
		return R.string.agenda;
	}

	public void onClick(View v) {
		int id = v.getId();
		
		Toast.makeText(getApplicationContext(), "Meu deus do c��ua", Toast.LENGTH_SHORT).show();
		
		switch(id)
		{
			case R.id.buttonAddNewItem:
					startActivity(new Intent(getApplicationContext(), AgendaItemCreationActivity.class));
				break;
			default:
				break;
		}		
	}

	
	public void onItemClick(AdapterView<?> parent, View view, int itemIndex, long id) {
		
		
		System.out.println("item index - " + itemIndex);
		System.out.println("item clicked view - " + view.toString());
		System.out.println("item id? - " + id);
		System.out.println("item parent - " + parent.toString());
		if(((CheckBox)((LinearLayout)view).getChildAt(0)).isChecked())
		{
			System.out.println("ta ta marcado");
			Toast.makeText(this, "item is done - ", Toast.LENGTH_SHORT).show();
		}
		else
		{
			System.out.println("nao ta marcado");
			Toast.makeText(this, "item is not done - ", Toast.LENGTH_SHORT).show();
		}
		
		Intent navigator = new Intent(this, AgendaItemCreationActivity.class);
		navigator.putExtra("agendaItem", itemsArrayAdapter.getItem(itemIndex));
		startActivity(navigator);
		
	}

	

	
}


